 
import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

function staffMgtCtrl($log, $http, $scope, $timeout, cfpLoadingBar) {
  'ngInject';

  let _this = this;

  this.yearList = [
    {
      id:'01',
      year:'2015'
    },
    {
      id:'02',
      year:'2016'
    }
  ]

  // 人员进出场情况汇总
  this.chartYearInit = () => {
    let option = {
      color: ['#59b0e7','#7cc78a','#a9a9a9'],
      title : {
        text: '人员进出场情况汇总',
        x:'center',
        textStyle: {color: '#666'}
      },
      tooltip : {
        trigger: 'axis'
      },
      legend: {
        data:['本月进场人数','本月离场人数','本月在线人数'],
        left:20,
        top:30
      },
      calculable: true,
      xAxis : [
        {
          type : 'category',
          data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
        }
      ],
      yAxis : [
        {
          show:false,
          type : 'value',
        }
      ],
      series : [
        {
          name:'本月进场人数',
          type:'bar',
          stack: '全部',
          barWidth : 30,
          itemStyle: {normal: {
            label : {show: true,
                    position:'top'
                  }
          }},
          data: _this.data4ChartShow.enterNum,
        },
        {
          name:'本月离场人数',
          type:'bar',
          stack: '全部',
          barWidth : 30,
          itemStyle: {normal: {
            label : {show: true,
                    position: 'bottom'
                  }
          }},
          data: _this.data4ChartShow.leaveNum,
        },
        {
          name:'本月在线人数',
          type:'line',
          label: {
                    normal: {
                      show: true,
                      position: 'top'
                    }
                  },
          data:_this.data4ChartShow.inNum,
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('personnel-total'));
    myChart.setOption(option);
  }

  // 人员教育培训状况
  this.chartTechInit = () => {
    var option = {
      tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },
      color: ['#59b0e7', '#ababab'],
      legend: {
        show:false,
        data: ['本月教育培训人数','本月未教育培训人数']
      },
      title : {
        text: '完成百分比',
        x:'center',
        y:'center',
        textStyle: {color: '#666'}
      },
      calculable : true,
      series : [
        {
          name:'完成百分比',
          type: 'pie',
          radius: ['60%', '72%'],
          data:[
            {value:this.numberY, name:'本月教育培训人数'},
            {value:this.numberN, name:'本月未教育培训人数'}
          ],
          label: {
            normal: {
              show: false,
            }
          },
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('staff-education'));
    myChart.setOption(option);
  }

  // 人员安全技术交底情况
  this.chartSafeInit = () => {
    var option = {
      tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },
      color: ['#59b0e7', '#ababab'],
      legend: {
        show:false,
        data: ['本月参加安全培训人数','本月未参加安全培训人数']
      },
      title : {
        text: '完成百分比',
        x:'center',
        y:'center',
        textStyle: {color: '#666'}
      },
      calculable : true,
      series : [
        {
          name:'完成百分比',
          type: 'pie',
          radius: ['60%', '72%'],
          data:[
            {value:this.safeY, name:'本月参加安全培训人数'},
            {value:this.safeN, name:'本月未参加安全培训人数'}
          ],
          label: {
            normal: {
              show: false,
            }
          },
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('maintenance-safe'));
    myChart.setOption(option);
  }

  var myDate = new Date();
  _this.years = myDate.getFullYear();

  this.listData = {};
  // 获取年进出场情况
  this.yearPersonal = () => {
    let respData = {
      date:_this.years
    } 
    $http.get(`${SERVER_CONTEXT}/api/safesite/workerInfo/countForWorkerHomePageByYear/`,
          {params:respData}
            )
            .then((data) => {
              this.list = [];
              for(key in data.data){
                this.list.push(data.data[key]);            
              }
              this.listData.enterNum = [];
              this.listData.inNum = [];
              this.listData.leaveNum = [];
              this.list.forEach((item) => {
                 this.listData.enterNum.push(item.enterNum);
                 this.listData.inNum.push(item.inNum);
                 this.listData.leaveNum.push(item.leaveNum);
              })

              this.data4ChartShow = {
                "enterNum": this.listData.enterNum,
                "inNum": this.listData.inNum,
                "leaveNum": this.listData.leaveNum
              } 

              this.chartYearInit();                           
            })          
  }

  // 获取人员教育培训情况
  this.techPersonal = () => {
    $http.get(`${SERVER_CONTEXT}/api/education/selectEducationWorker/1`
            )
            .then((resp) => {              
              this.educationStatistics = [];              
              for(key in resp.data){
                this.educationStatistics.push(resp.data[key]);
              }
              this.numberN = this.educationStatistics[1];
              this.numberY = this.educationStatistics[0];
              this.chartTechInit();
            });   
  }

  // 获取人员安全技术交底情况
  this.safePersonal = () => {
    $http.get(`${SERVER_CONTEXT}/api/education/selectEducationWorker/2`
            )
            .then((resp) => {   
              this.safeStatistics = [];              
              for(key in resp.data){
                this.safeStatistics.push(resp.data[key]);
              }
              this.safeN = this.safeStatistics[1];
              this.safeY = this.safeStatistics[0];
              this.chartSafeInit();
            });   
  }

  // 点击年
  this.syearOne = (param) => {
    _this.years = param.year;
    param.acitve = true;
    _this.years = param.year;
    this.yearPersonal();
  }

  // 获取当前时间变色
  this.getDate = (param) => {
    // 当前时间
    this.yearList.forEach((item) => {
      if (item.year == _this.years) {
        item.active = true;
      }
    })
  }

  // 初始化页面
  this.initPage = () => {

    // 获取当前时间变色
    this.getDate();

    // 获取人员进出场情况汇总
    this.yearPersonal();

    // 获取人员教育培训情况
    this.techPersonal();

    // 获取人员安全技术交底情况
    this.safePersonal();

  }

  this.initPage();

}

export default staffMgtCtrl;